//
//  XJ_RealNameAuthView.m
//  XunJieProject
//
//  Created by xue on 2018/6/13.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_RealNameAuthView.h"
///alertView  宽
#define AlertW SCREEN_WIDTH-40
@interface XJ_RealNameAuthView()
// 背景视图
@property (nonatomic, strong) UIView *backgroundView;
//弹窗
@property (nonatomic,retain) UIView *alertView;
//内容
@property (nonatomic,retain) UILabel *msgLbl;
//确认按钮
@property (nonatomic,retain) UIButton *sureBtn;
//取消按钮
@property (nonatomic,retain) UIButton *cancleBtn;
@property (nonatomic,retain) NSString *messageStr;

@end
@implementation XJ_RealNameAuthView

- (instancetype)initWithTitle:(NSString*)tittle Message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle
{
    self = [super init];
    if (self) {
        self.messageStr =message;
        self.frame = [UIScreen mainScreen].bounds;
        [self addSubview:self.backgroundView];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 10.0;
        self.alertView.clipsToBounds =YES;
        
        self.alertView.frame = CGRectMake(0, 0, AlertW, 240);
        self.alertView.layer.position = self.center;
        
        
        UILabel *title =[[UILabel alloc]initWithFrame:CGRectMake(20, 10, AlertW-2*20, 40)];
        title.text =tittle;
        title.font =[UIFont systemFontOfSize:16.0];
        title.textColor =UIColorFromRGB(0x14A0D5);
        title.textAlignment = NSTextAlignmentLeft;
        title.numberOfLines=0;
        [self.alertView addSubview:title];
        
        
        
        UIView *line1 =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(title.frame), AlertW, 1)];
        line1.backgroundColor =UIColorFromRGB(0x14A0D5);
        [self.alertView addSubview:line1];
        
        
        self.msgLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(line1.frame), AlertW-2*20, 50)];
        self.msgLbl.text =message;
        self.msgLbl.font =[UIFont systemFontOfSize:14.0];
        self.msgLbl.textColor =[UIColor blackColor];
        self.msgLbl.textAlignment = NSTextAlignmentLeft;
        self.msgLbl.numberOfLines=0;
        [self.alertView addSubview:self.msgLbl];
        
        UIView *line2 =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_msgLbl.frame)+8, AlertW, 1)];
        line2.backgroundColor =Color_Line;
        [self.alertView addSubview:line2];
        
        
        //两个按钮
        if (cancleTitle && sureTitle) {
            self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(line2.frame), CGRectGetWidth(self.alertView.frame)/2, 50);
//            _cancleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
            [self.cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.cancleBtn.tag = 1;
            [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            self.cancleBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
            [self.alertView addSubview:self.cancleBtn];
            
            
            
            
            
            self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            self.sureBtn.frame = CGRectMake(CGRectGetWidth(self.alertView.frame)/2, CGRectGetMaxY(line2.frame), CGRectGetWidth(self.alertView.frame)/2, 50);
            _sureBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
            [self.sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.sureBtn.tag = 2;
            self.sureBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
            [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            [self.alertView addSubview:self.sureBtn];
            
            
            UIView *line3 =[[UIView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.alertView.frame)/2, CGRectGetMaxY(line2.frame), 1, 50)];
            line3.backgroundColor =Color_Line;
            [self.alertView addSubview:line3];
            
        }
        
        ///只有取消按钮
        if (cancleTitle && !sureTitle) {
            
            self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(line2.frame), CGRectGetWidth(self.alertView.frame), 50);
            _cancleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
            [self.cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.cancleBtn.tag = 1;
            [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            self.cancleBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
            [self.alertView addSubview:self.cancleBtn];
            
            
        }
        
//        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
//        button.backgroundColor =ClearColor;
//        button.frame =CGRectMake(0, CGRectGetMaxY(line2.frame), AlertW, 50);
//        [button setTitle:@"确定" forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        button.titleLabel.font =[UIFont systemFontOfSize:15.0];
//        [button addTarget:self action:@selector(Click_hidden) forControlEvents:UIControlEventTouchUpInside];
//        [self.alertView addSubview:button];
        
        //计算高度
        CGFloat alertHeight = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
        self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
        [self addSubview:self.alertView];
    }
    return self;
}

#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _backgroundView.userInteractionEnabled = YES;
        if ([self.messageStr isEqualToString:@"确定退出吗？"]) {
            UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
            [_backgroundView addGestureRecognizer:myTap];

        }
    }
    return _backgroundView;
}
#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
    }];
}

#pragma mark - 弹出 -
- (void)showXLAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
    //    [self performSelector:@selector(Click_hidden) withObject:nil afterDelay:1.8];
}
-(void)Click_hidden{
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    //    [self removeFromSuperview];
}
#pragma mark - 回调 -设置只有2  -- > 确定才回调
- (void)buttonEvent:(UIButton *)sender
{
    if (self.resultIndex) {
        self.resultIndex(sender.tag);
    }
    [self Click_hidden];
}
@end
